<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="600px"
      :close-on-click-modal="false"
      @close="resetForm('form')"
      append-to-body
    >
      <div class="education-add-form">
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="父节点" prop="pid">
            <ParendSelect
              v-if="options.length && dialogVisible"
              :options="options"
              :defaultValue="form.pid"
              @emit="setPid"
            />
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model.trim="form.name"></el-input>
          </el-form-item>
          <el-form-item label="简介">
            <el-input type="textarea" v-model.trim="form.content"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { sysIdeologicalEducationAdd } from "@/api/admin/system/sysIdeologicalEducation.js";
import Bus from "@/utils/bus.js";
export default {
  components: {
    CoverImg: () => import("@/components/coverImg"),
    ParendSelect: () => import("./parendSelect.vue"),
  },
  data() {
    return {
      title: "新增",
      dialogVisible: false,
      form: {
        pid: "",
        name: "",
        content: "",
        auditStatus: 1,
        orderBy: "10",
        status: "0",
        type: 3,
        level: 3,
        // definitionCode: "21",
        // scope: this.$store.getters.schoolDeptId,
      },
      rules: {
        pid: [{ required: true, message: "请选择父节点", trigger: "blur" }],
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
      options: [],
    };
  },
  mounted() {
    Bus.$on("showAddDialog_EducationAdd", (v) => {
      this.options = v;
      this.dialogVisible = true;
    });
  },
  beforeDestroy() {
    Bus.$off("showAddDialog_EducationAdd");
  },
  methods: {
    setPid(v) {
      this.form.pid = v;
    },
    //提交
    submitForm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.add();
        }
      });
    },
    //新增
    add() {
      let { schoolDeptId } = this.$store.getters;
      if (schoolDeptId) {
        Object.assign(this.form, {
          auditStatus: 1,
          definitionCode: "21",
          scope: schoolDeptId,
        });
      } else {
        this.form.auditStatus = 2;
        delete this.form.definitionCode;
        delete this.form.scope;
      }
      sysIdeologicalEducationAdd(this.form).then((res) => {
        if (res.data.code == 0) {
          this.dialogVisible = false;
          this.$message({
            message: "已添加，请等待审核结果",
            type: "success",
          });
        }
      });
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields();
      this.form.pid = "";
      this.form.name = "";
      this.form.content = "";
      this.form.auditStatus = 1;
      delete this.form.definitionCode;
      delete this.form.scope;
    },
  },
};
</script>
<style scoped>
.el-form-item .el-form-item {
  margin-bottom: 22px;
}
</style>